package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;

import com.atguigu.gmall.product.entity.SkuImage;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.*;
import com.atguigu.gmall.product.vo.SkuInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class SkuController {

    @Autowired
    SpuImageService spuImageService;
    @Autowired
    SpuSaleAttrService spuSaleAttrService;
    @Autowired
    BaseSaleAttrService baseSaleAttrService;
    @Autowired
    SkuInfoService skuInfoService;
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable("spuId")Long spuId){
        QueryWrapper<SpuImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_id",spuId);
        List<SpuImage> list = spuImageService.list();
        return Result.ok(list);
    }
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable("spuId")long spuId){
        List<SpuSaleAttr> list = baseSaleAttrService.getSpuSaleAttrAndValueList(spuId);
        return Result.ok(list);
    }
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfoVo skuInfoVo){
        skuInfoService.saveSkuInfo(skuInfoVo);
        return Result.ok();
    }
    @GetMapping("/list/{page}/{limit}")
    public Result page(@PathVariable("page") Long pn,
                          @PathVariable("limit") Long ps){
        Page<SkuInfo> skuInfoPage = new Page<>(pn,ps);
        return Result.ok(skuInfoService.page(skuInfoPage));
    }
    @DeleteMapping("/deletesku/{skuId}")
    public Result deleteSku(@PathVariable("skuId")Long skuId){
        skuInfoService.removeSku(skuId);

        return Result.ok();
    }
}
